import Vue from 'vue'
// import VueRouter from 'vue-router'
import Router from 'vue-router'
import HomeView from '../views/HomeView.vue'
// import store from "@/store";

// Vue.use(VueRouter)
Vue.use(Router)

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  }
]

// const router = new VueRouter({
//   mode: 'history',
//   base: process.env.BASE_URL,
//   routes
// })

// export default router



const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}



/*
import { loadMicroApp } from 'qiankun'

let apps = [
  {
    name: 'child-app1',
    entry: 'http://localhost:9091',
    container: '#child-app1',
    routeStartPath:'/child-app1',
    loaded: false,
    props: { 
      data : {
        root:window.vm, 
        store, 
        router,
        callback:(e)=>{
          console.log("child-app1 挂载回调成功！",e);
          (e.components ||[]).forEach(module=>{
            console.log(module, module.name,  module.component);
            const name = module.name
            name && Vue.component(name, module.component.default)
          });
          store.commit('setApp1Callback',true)
        }
      }
    }
  },{
    name: 'child-app2',
    entry: 'http://localhost:9092',
    container: '#child-app2',
    routeStartPath:'/child-app2',
    loaded: false
  }
]

let microApps = {}

router.beforeEach(async(to, from, next) => {
  await loadChild(to.path)
  next()
})

//根据path加载相应微应用
function loadChild(path){
  return new Promise((resolve)=>{
    let isLoad = false
    apps.some((ele, index) => {
      if (path.startsWith(ele.routeStartPath)) {
        if (!ele.loaded) {
          isLoad = true
            
          // 手动加载微应用
          microApps[ele.name] = loadMicroApp({
            ...ele}, {
            strictStyleIsolation: true,
            experimentalStyleIsolation: true
          })

          // 加载微应用完成回调
          microApps[ele.name].loadPromise.then(() => {
            resolve()
            apps[index].loaded = true
          }).catch(() => {
            window.vm.$message.error(ele.name+'微应用加载失败')
          })
            
        }
      }      
    }) 

    if (!isLoad) {
        resolve()
    }
  })
}
*/

export default router
